<?php

namespace App\View\Components;

use Illuminate\Http\Request;
use Illuminate\View\Component;

class AdminSidebar extends Component
{
    public $page_name = '';
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct(Request $request, $page_name = '')
    {
        $this->page_name = $page_name;
        $this->se = session()->get('account');
        $this->getPageName($request);
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\View\View|string
     */
    public function render()
    {
        $user_type = session('admin.user_type');
        if ($user_type == 1) {
            $menu = $this->getMenu();
        } else {
            $menu = $this->getDistributorMenu();
        }

        return view('components.admin-sidebar', ['menu' => $menu]);
    }
    protected function getPageName(Request $request)
    {
        if (!empty($this->page_name)) {
            return ;
        }
        $path = $request->path();
        $prefix = 'admin/';
        $path = substr($path, mb_strlen($prefix)); // online
        $base_path = ['distributor', 'dealer', 'customer'];
        if (empty($path)) {
            $this->page_name = 'index';
        }
        foreach ($base_path as $k => $v) {
            if (strstr($path,$v.'/')===0 || $path==$v) {
                $this->page_name = $v;
            }
        }
        /* $online = ['online'];
         if (strstr($path,'online/')===0 || in_array($path, $online)) {
             $this->page_name = 'online';
         }*/
    }

    protected function getDistributorMenu()
    {
        $prefix = '/admin/';
        $menu = [
            [
                'icon' => 'home',
                'text' => ts('Dashboard'),
                'page_name' => ['index'],
            ],
            /*[
                'icon' => 'user-o',
                'text' => ts('managedistributor'),
                'url' => 'distributor',
                'page_name' => ['distributor'],
                'sub' => [
                    [
                        'text' => ts('managedistributor'),
                        'url' => 'distributor',
                    ],
                    [
                        'text' => ts('adddistributor'),
                        'url' => 'distributor/create'
                    ],
                ]
            ],*/
            [
                'icon' => 'user-o',
                'text' => ts('MANAGEDEALERS'),
                'url' => 'dealer',
                'page_name' => ['dealer'],
                'sub' => [
                    [
                        'text' => ts('MANAGEDEALERS'),
                        'url' => 'dealer',
                    ],
                    [
                        'text' => ts('addDEALER'),
                        'url' => 'dealer/create'
                    ],
                ]
            ],
            [
                'icon' => 'user-o',
                'text' => ts('MANAGECAROWNERS'),
                'url' => 'customer',
                'page_name' => ['customer'],
                'sub' => [
                    [
                        'text' => ts('VIEWCAROWNERS'),
                        'url' => 'customer',
                    ],
                ]
            ],
            [
                'icon' => 'home',
                'text' => ts('MANAGEPRODUCT'),
                'url' => 'rolls',
                'page_name' => ['rolls'],
                'sub' => [
                    [
                        'text' => ts('VIEWROLLS'),
                        'url' => 'rolls',
                    ],
                    [
                        'text' => ts('ADDROLL'),
                        'url' => 'rolls/create',
                    ],
                    [
                        'text' => ts('FAQIDIAOHUO'),
                        'url' => 'rolls/transfer',
                    ]
                ]
            ],
            [
                'icon' => 'home',
                'text' => ts('MANAGEWARRANTY'),
                'url' => 'warranty',
                'page_name' => ['warranty'],
                'sub' => [
                    [
                        'text' => ts('MANAGEWARRANTY'),
                        'url' => 'warranty',
                    ],
                    [
                        'text' => ts('ADDWARENTY'),
                        'url' => 'warranty/create',
                    ],
                    [
                        'text' => ts('BULKIMPORT'),
                        'url' => 'warranty/bulk_import'
                    ]
                ]
            ],
            /*[
                'icon' => 'cog',
                'text' => ts('SETTING'),
                'url' => '',
                'page_name' => ['cities', 'province', 'region'],
                'sub' => [
                    [
                        'text' => ts('MANAGE').' '.ts('CITY'),
                        'url' => 'setting/cities',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('PROVINCE'),
                        'url' => 'setting/province',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('REGION'),
                        'url' => 'setting/region',
                    ],
                    [
                        'text' => ts('STATICTEXTMANAGEMENT'),
                        'url' => 'setting/macro_management',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('FILMTYPE'),
                        'url' => 'setting/filmtype'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('MAKE'),
                        'url' => 'setting/make'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('MODEL'),
                        'url' => 'setting/model'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('PART'),
                        'url' => 'setting/part'
                    ],
                ]
            ],*/
        ];

        $da =[];
        foreach ($menu as $k => $v) {
            if (!empty($v['level'])) {

            }
            if (empty($v['external'])) {
                $v['url'] = $prefix . @$v['url'];
            } else {
                $v['url'] = @$v['url'];
            }
            if (!empty($v['sub'])) {
                foreach ($v['sub'] as $k2 => $v2) {
                    if (empty($v2['external'])) {
                        $v['sub'][$k2]['url'] = $prefix . @$v2['url'];
                    }
                }
            }
            $da[] = $v;
        }
        return $da;
    }

    protected function getMenu()
    {
        $prefix = '/admin/';
        $menu = [
            [
                'icon' => 'home',
                'text' => ts('Dashboard'),
                'page_name' => ['index'],
            ],
            [
                'icon' => 'user-o',
                'text' => ts('managedistributor'),
                'url' => 'distributor',
                'page_name' => ['distributor'],
                'sub' => [
                    [
                        'text' => ts('managedistributor'),
                        'url' => 'distributor',
                    ],
                    [
                        'text' => ts('adddistributor'),
                        'url' => 'distributor/create'
                    ],
                ]
            ],
            [
                'icon' => 'user-o',
                'text' => ts('MANAGEDEALERS'),
                'url' => 'dealer',
                'page_name' => ['dealer'],
                'sub' => [
                    [
                        'text' => ts('MANAGEDEALERS'),
                        'url' => 'dealer',
                    ],
                    [
                        'text' => ts('addDEALER'),
                        'url' => 'dealer/create'
                    ],
                ]
            ],
            [
                'icon' => 'user-o',
                'text' => ts('MANAGECAROWNERS'),
                'url' => 'customer',
                'page_name' => ['customer'],
                'sub' => [
                    [
                        'text' => ts('VIEWCAROWNERS'),
                        'url' => 'customer',
                    ],
                ]
            ],
            [
                'icon' => 'home',
                'text' => ts('MANAGEPRODUCT'),
                'url' => 'rolls',
                'page_name' => ['rolls'],
                'sub' => [
                    [
                        'text' => ts('VIEWROLLS'),
                        'url' => 'rolls',
                    ],
                    [
                        'text' => ts('ADDROLL'),
                        'url' => 'rolls/create',
                    ],
                    [
                        'text' => ts('FAQIDIAOHUO'),
                        'url' => 'rolls/transfer',
                    ],
                    [
                        'text' => ts('BULKIMPORT'),
                        'url' => 'warranty/bulk_import'
                    ],
                ]
            ],
            [
                'icon' => 'home',
                'text' => ts('MANAGEWARRANTY'),
                'url' => 'warranty',
                'page_name' => ['warranty'],
                'sub' => [
                    [
                        'text' => ts('MANAGEWARRANTY'),
                        'url' => 'warranty',
                    ],
                    [
                        'text' => ts('ADDWARENTY'),
                        'url' => 'rolls',
                    ],
                    [
                        'text' => ts('BULKIMPORT'),
                        'url' => 'warranty/bulk_import'
                    ],
                ]
            ],
            [
                'icon' => 'cogs',
                'text' => '操作记录',
                'url' => 'logs'
            ],
            [
                'icon' => 'cog',
                'text' => ts('SETTING'),
                'url' => '',
                'page_name' => ['cities', 'province', 'region'],
                'sub' => [
                    [
                        'text' => ts('MANAGE').' '.ts('CITY'),
                        'url' => 'setting/cities',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('PROVINCE'),
                        'url' => 'setting/province',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('REGION'),
                        'url' => 'setting/region',
                    ],
                    [
                        'text' => ts('STATICTEXTMANAGEMENT'),
                        'url' => 'setting/macro_management',
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('FILMTYPE'),
                        'url' => 'setting/filmtype'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('MAKE'),
                        'url' => 'setting/make'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('MODEL'),
                        'url' => 'setting/model'
                    ],
                    [
                        'text' => ts('MANAGE').' '.ts('PART'),
                        'url' => 'setting/part'
                    ],
                ]
            ],
        ];

        $da =[];
        foreach ($menu as $k => $v) {
            if (!empty($v['level'])) {

            }
            if (empty($v['external'])) {
                $v['url'] = $prefix . @$v['url'];
            } else {
                $v['url'] = @$v['url'];
            }
            if (!empty($v['sub'])) {
                foreach ($v['sub'] as $k2 => $v2) {
                    if (empty($v2['external'])) {
                        $v['sub'][$k2]['url'] = $prefix . @$v2['url'];
                    }
                }
            }
            $da[] = $v;
        }
        return $da;
    }
}
